package com.jiangjiang.eshop.commodity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangjiang.eshop.commodity.dto.BrandDTO;
import com.jiangjiang.eshop.commodity.dto.BrandQueryPageReq;
import com.jiangjiang.eshop.commodity.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName BrandController
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/13 21:20
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/commodity/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 查询品牌分页数据
     * @param req 查询条件
     * @return 查询结果
     */
    @PostMapping("/queryForPage")
    public Page<BrandDTO> queryForPage(@RequestBody BrandQueryPageReq req) {
        try {
            return brandService.queryForPage(req);
        } catch (Exception e) {
            log.error("BrandController queryForPage error", e);
        }
        return null;
    }


    /**
     * 根据id查询品牌
     * @param id 品牌id
     * @return 品牌
     */
    @GetMapping("/queryById")
    public BrandDTO queryById(@RequestParam("id") Long id) {
        try {
            return brandService.queryById(id);
        } catch (Exception e) {
            log.error("BrandController queryById error", e);
            return null;
        }
    }

    /**
     * 新增品牌
     * @param dto 品牌入仓
     * @param logoFile 品牌logo文件
     * @param authVoucherFile  品牌授权认证图片
     * @return 处理结果
     */
    @PostMapping("/save")
    public Boolean saveBrand(BrandDTO dto, MultipartFile logoFile,MultipartFile authVoucherFile){
        try {
            return brandService.saveBrand(dto, logoFile, authVoucherFile);
        }catch (Exception e){
            log.error("BrandController saveBrand error",e);
        }
        return false;
    }

    /**
     * 更新品牌
     * @param brand 品牌数据
     * @return 处理结果
     */
    @PutMapping("/updateBrand")
    public Boolean updateBrand(@RequestBody BrandDTO brand) {
        try {
            return  brandService.updateBrand(brand);
        } catch (Exception e) {
            log.error("BrandController updateBrand error", e);
            return false;
        }
    }

    /**
     * 删除品牌
     * @param id 品牌id
     * @return 处理结果
     */
    @GetMapping("/removeBrand")
    public Boolean removeBrand(@RequestParam("id") Long id) {
        try {
            return brandService.removeBrand(id);
        } catch (Exception e) {
            log.error("BrandController removeBrand error", e);
            return false;
        }
    }
}
